<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="应用名称" value="appName"></el-option>
              <el-option label="批次" value="batch"></el-option>
              <el-option label="操作者" value="username"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef" max-height="535">
      <el-table-column v-if="colData[0].istrue" align="center" prop="batchName" label="批次"></el-table-column>
      <el-table-column v-if="colData[1].istrue"  align="center" prop="batchAdmin" label="归属账号"></el-table-column>
      <el-table-column v-if="colData[2].istrue"  align="center" prop="app_name" label="应用名称">
        <template slot-scope="scope">
          <el-select v-show="scope.row.appIdArr.length>0" v-model="scope.row.appIdArr" placeholder="请选择" multiple disabled>
            <el-option
              v-for="item in scope.row.appArr"
              :key="item.app_id"
              :label="item.app_name"
              :value="item.app_id.toString()">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue"  align="center" prop="app_package" label="包名">
        <template slot-scope="scope">
          <el-select v-show="scope.row.appIdArr.length>0" v-model="scope.row.appIdArr" placeholder="请选择" multiple disabled>
            <el-option
              v-for="item in scope.row.appArr"
              :key="item.app_id"
              :label="item.bg"
              :value="item.app_id.toString()">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue"  align="center" prop="type" label="类型">
        <template slot-scope="scope">
          <span>消息白名单</span>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue"  align="center" prop="edit_time" label="更新时间">
        <template slot-scope="scope">{{scope.row.edit_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue"  align="center" prop="admin_name" label="操作账号"></el-table-column>
      <el-table-column  align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加dialog表单 -->
    <el-dialog :title="editSubjects ? '消息通知管控' : '编辑'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="35%" @close="addFormclose">
      <el-form :model="addForm" ref="ruleForm" :rules="rules" style="padding-top:0">
        <el-form-item label="批次:" label-width="80px" prop="batch">
          <el-select v-model="addForm.batch" placeholder="请选择批次" class="add_select" :disabled="!editSubjects">
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用:" label-width="80px" prop="app">
          <el-select v-model="addForm.app" placeholder="请选择应用" class="add_select" value-key="app_id" multiple filterable>
            <el-option
              v-for="(item) in appOptions"
              :key="item.app_id"
              :label="item.app_name"
              :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" @click="getaddAppControl('ruleForm')" size="mini" round>确 定</el-button>
        <el-button v-else type="primary" @click="getEditAppMessage('ruleForm')" size="mini" round>编辑</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
  import dynamicTable from "@/components/DynamicTable";
  import deleteDialog from "@/components/deleteDialog";
  import { getBatchTwo } from "@/api/equipment.js";
  import tableMini from '@/utils/tableMini.js'
  import {
    addAppMessageControl,
    delAppMessageControl,
    editAppMessageControl, gelAllApp,
    queryAppMessageControl
  } from "@/api/deployment";
  export default {
    mixins: [tableMini],
    components: { dynamicTable, deleteDialog },
    data() {
      return {
        checkboxVal: ["批次", "归属账号", "应用名称", "包名", "类型", "更新时间", "操作账号"],
        //归属table
        tableData: [],
        select: "", //搜索条件
        listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), appName: undefined, appPackage: undefined, username: undefined },
        //添加按钮控制
        dialogFormVisible: false,
        //编辑按钮控制
        editSubjects: true,
        //浏览器专题id
        subjectId: 0,
        //家长SN码下拉选择
        lotoptions: [],
        appOptions: [],
        //添加表单
        addForm: { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), batch: "", app: "" },
        rules: { batch: [{ required: true, message: "请选择批次", trigger: "blur" }], app: [{ required: true, message: "请选择应用", trigger: "blur" }] }
      }
    },
    created() {
      this.getTabularData()
      this.getAllApp()
      this.getBatchTwos()
    },
    methods: {
      getTabularData() {
        queryAppMessageControl(this.listQuery).then(res => {
          this.tableData = res.data
          this.total = res.total
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      //删除按钮事件
      deleteEvents() {
        let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
        delAppMessageControl(params).then(res => {
          this.$message.success(res.msg)
          this.getTabularData()
        })
      },
      //根据ID查询霸屏
      openEdit(row) {
        this.editSubjects = false
        this.subjectId = row.id
        this.dialogFormVisible = true
        this.addForm.batch = row.batch_id+''
        this.addForm.app = row.appArr
      },
      getEditAppMessage(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.id = this.subjectId
            editAppMessageControl(addForm).then(res => {
              this.dialogFormVisible = false
              this.$message.success(res.msg)
              this.getTabularData()
            }).catch(err => { })
          } else {
            return false
          }
        })
      },
      //增加应用霸屏
      getaddAppControl(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addAppMessageControl(addForm).then(res => {
              this.$message.success(res.msg)
              this.getTabularData()
              this.dialogFormVisible = false
            }).catch(err => {})
          } else {
            return false
          }
        })
      },
      //添加按钮事件
      btnaddBrand() {
        this.editSubjects = true
        this.dialogFormVisible = true
      },
      //根据ID获取批次列表
      getBatchTwos() {
        let data = { userId: localStorage.getItem("ID") }
        getBatchTwo(data).then(res => {
          this.lotoptions = res.data
        }).catch(err => {
          this.lotoptions = []
        })
      },
      //根据ID获取应用
      getAllApp() {
        let data = { userId: localStorage.getItem("ID"), type:2}
        gelAllApp(data).then(res => {
          this.appOptions = res.data
        }).catch(err => {
          this.appOptions = []
        })
      },
      addFormclose() {
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.addForm = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), batch: "", app: "" }
        })
      }
    }
  }
</script>
